/**
  * Simple OpenVG example which renders a bitmap background and a string of spheres following a
  * sine curve.
  *
  * This code runs about 25 fps on Nokia 500 device, 60 fps on Nokia E7 device.
  *
  */

#include "game.h"
#include <qDebug>

// for readability, the screen and texture sizes are hard-coded
const float BACKGROUND_WIDTH=1024;
const float BALL_WIDTH=128.0f;
const float BALL_HEIGHT=128.0f;
const float SCREEN_WIDTH=640.0f;
const float SCREEN_HEIGHT=360.0f;

const float BACKGROUND_SCROLL_SPEED=50.0f;
const float BALL_PHASE=0.0111f;
const float BALL_SCALE=120.0f;

// image resources
VGImage ball;
VGImage bg;

/** walltime */
float t;


void game_init()
{
}

void game_destroy()
{
}

void game_prepare()
{
    vgSeti(VG_RENDERING_QUALITY, VG_RENDERING_QUALITY_BETTER);
    vgSeti(VG_IMAGE_QUALITY, VG_IMAGE_QUALITY_BETTER);

    ball = loadVGImage("images/planet.png");
    bg = loadVGImage("images/orange_bg_mountain.png");
}

void game_release()
{
    vgDestroyImage(ball); ball=VG_INVALID_HANDLE;
    vgDestroyImage(bg); bg=VG_INVALID_HANDLE;
}

void game_update(float walltime)
{
    t=walltime;
}

void game_render()
{
    // clear background using black
    VGfloat color[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
    vgSetfv( VG_CLEAR_COLOR, 4, color );
    vgClear( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

    // render scrolling background bitmap (twice for looping effect)
    vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
    vgLoadIdentity();
    vgTranslate(-fmod(t*BACKGROUND_SCROLL_SPEED,BACKGROUND_WIDTH), 0);
    vgDrawImage(bg);
    vgTranslate(BACKGROUND_WIDTH, 0);
    vgDrawImage(bg);

    // render sine spheres
    for(int i=-BALL_WIDTH/2;i<SCREEN_WIDTH+BALL_WIDTH/2;i+=BALL_WIDTH/4)
    {
        vgLoadIdentity();
        vgTranslate(i, (SCREEN_HEIGHT-BALL_HEIGHT)/2+sin(t+i*BALL_PHASE)*BALL_SCALE);
        vgDrawImage(ball);
    }
}

void game_event(int type, float x, float y, float z)
{
}
